<template>
	<view>
		<view class="cu-custom" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar search fixed" :style="style" :class="[bgImage!=''?'none-bg text-white bg-img':'',bgColor]">
				<view class="action text-white" @tap="BackPage" v-if="isBack">
					<text class="cuIcon-back"></text>
					<slot name="backText"></slot>
				</view>
				<view class="text-white text-center margin-left-20" v-else>
					<view>{{weather.city}}</view>
					<view>{{weather.weather}} {{weather.temperature}} ℃</view>
				</view>
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input @focus="InputFocus" v-if="isSearch" v-model="text" focus="true" @input="changeInput" :adjust-position="false" type="text" :placeholder="placeholder" confirm-type="search"></input>
					<input @focus="InputFocus" v-else @tap="toSearch" :adjust-position="false" type="text" :placeholder="placeholder" confirm-type="search"></input>
				</view>
				<view class="action">
					<slot name="right"></slot>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				InputBottom: 0,
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				text: ''
			};
		},
		name: 'cu-bar',
		computed: {
			style() {
				var StatusBar= this.StatusBar;
				var CustomBar= this.CustomBar;
				var bgImage = this.bgImage;
				var style = `height:${CustomBar}px;padding-top:${StatusBar}px;`;
				if (this.bgImage) {
					style = `${style}background-image:url(${bgImage});`;
				}
				return style
			}
		},
		props: {
			bgColor: {
				type: String,
				default: ''
			},
			isBack: {
				type: [Boolean, String],
				default: false
			},
			bgImage: {
				type: String,
				default: ''
			},
			weather: {
				type:Object,
				default:null
			},
			placeholder: {
				type:String,
				default:''
			},
			isSearch: {
				type: [Boolean, String],
				default:false
			}
		},
		created() {
			if(this.isBack == 0){
				
			}
		},
		methods: {
			InputFocus(e) {
				this.InputBottom = e.detail.height
			},
			InputBlur(e) {
				this.InputBottom = 0
			},
			BackPage() {
				uni.navigateBack({
					delta: 1
				});
			},
			toSearch(){
				uni.navigateTo({
					url:'/pages/home/search'
				})
			},
			changeInput(){
				this.$emit('search',this.text)
			}
		}
	}
</script>

<style>
.margin-left-20{
	margin-left: 20rpx;
}
.cu-custom .cu-bar .sw-bar-content {
	width: calc(100% - 220upx) !important;
}
</style>
